package com.lizardmind.haofudoctor.ui.aboutMe

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.util.ArrayMap
import android.view.View
import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.ImageUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.ke
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityFeedbackBinding
import com.lizardmind.haofudoctor.databinding.ItemSelectImgBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast

/**
 * 意见反馈界面
 * Created by Extends on 2017/11/1 15:28
 */
class FeedbackActivity :BaseActivity() {
    val binding by lazy { getDataBinding<ActivityFeedbackBinding>(R.layout.activity_feedback) }
    val adapter by lazy { BaseAdapter<LocalMedia,ItemSelectImgBinding>(R.layout.item_select_img, arrayListOf(LocalMedia())) }
    val noNullList = arrayListOf<LocalMedia>()
    override fun onCreatRootView(): View = binding.root

    override fun onInitView(savedInstanceState: Bundle?) {
        binding.apply {
            details=""
            maxCount=500

            //初始化标题栏
            feedbackTitle.apply {
                titleText.text = "意见反馈"
                imgLeft.setImageResource(R.mipmap.back)
                imgLeft.onClick { finish() }
            }

            feedbackImg.apply {
                layoutManager = GridLayoutManager(this@FeedbackActivity,3)
                adapter = this@FeedbackActivity.adapter
            }

            adapter.onBind { itemBingding, position, data ->
                if(data.path==null){
                    itemBingding.itemSiImg.setImageResource(R.mipmap.picture_add)
                }
                itemBingding.itemSiImg.onClick {
                    if(data.path==null){
                        //点击选择图片
                        PictureSelector.create(this@FeedbackActivity)
                                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                                .maxSelectNum(3)
                                .previewImage(true)// 是否可预览图片 true or false
                                .isCamera(true)// 是否显示拍照按钮 true or false
                                .compress(true)// 是否压缩 true or false
                                .selectionMedia(noNullList)
                                .forResult(PictureConfig.CHOOSE_REQUEST)
                    }else{
                        //查看大图
                        PictureSelector.create(this@FeedbackActivity).externalPicturePreview(position,noNullList)
                    }
                }
                itemBingding.itemSiCancel.onClick {
                    noNullList.remove(data)
                    adapter.datas.remove(data)
                    if(noNullList.size==2){
                        adapter.datas.add(LocalMedia())
                    }
                    adapter.notifyDataSetChanged()
                }
            }

            feedbackOk.onClick {
                var temp =""
                val map = hashMapOf("action" to "opinion",
                        "token" to DoctorInfo.token,
                        "content" to details!!)
                if(noNullList.size>0){
                    (0 until noNullList.size)
                            .map { ImageUtils.bitmap2Bytes(ImageUtils.getBitmap(noNullList[it].compressPath), Bitmap.CompressFormat.PNG) }
                            .map { EncodeUtils.base64Encode2String(it) }
                            .forEach { temp += ","+ it }
                    temp = temp.substring(1)
                    map.put("pictures",temp)
                }
                observe {
                    postApi<Request<String>>(map, Consumer {
                        it.dispose {
                            toast(it)
                            finish()
                        }
                    },context=this@FeedbackActivity)
                }
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== Activity.RESULT_OK){
            if(requestCode==PictureConfig.CHOOSE_REQUEST){
                val selectList = PictureSelector.obtainMultipleResult(data)
                ke(selectList[0].compressPath,selectList[0].path)
                noNullList.clear()
                noNullList.addAll(selectList)
                if(selectList.size<3){
                    selectList.add(LocalMedia())
                }
                adapter.clearAddAllData(selectList)
            }
        }
    }
}